#!/usr/bin/python
# *-* coding: utf-8 *-*

from pyEcodex2 import ECODEX

fac = 'factest.xml'
rfc = 'AAA010101AAA'
xml = open(fac).read().decode('utf-8')

# Si usas solo timbrado
# pac = ECODEX(rfc, True)
pac = ECODEX(rfc)
pac.xml_send = xml

# Es IMPORTANTE, enviar a timbrar con un ID recuperable en tu sistema
# pac.TimbrarXML(id_recuperable):
if pac.TimbrarXML():
    f = open('Timbrada-%s' % fac, 'w')
    f.write(pac.xml)
    f.close()
    print ('Factura timbrada correctamente')
else:
    print (pac.error)